package com.one.service.impl;

import com.one.mapper.StoreMapper;
import com.one.mapper.UserTrasRecordMapper;
import com.one.pojo.Store;
import com.one.pojo.UserTrasRecord;
import com.one.service.StoreService;
import com.one.service.UserTrasRecordService;
import com.one.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description
 * @Author semstouch
 * @Date 2017/4/7
 **/
@Service
public class UserTrasRecordServiceImpl implements UserTrasRecordService {
    @Resource
    private UserTrasRecordMapper userTrasRecordMapper;
    @Resource
    private StoreMapper storeMapper;


    @Override
    public String add(UserTrasRecord userTrasRecord) {
        Store sendStore =storeMapper.findByUserId(userTrasRecord.getSenderId());
        userTrasRecord.setId(StringUtils.getUUID());
        if(userTrasRecord.getGiftType().equals("1")&&sendStore.getChicksNum().compareTo(userTrasRecord.getGiftNum())==-1){
            return "帐号小鸡数量不足！";
        }
        if(userTrasRecord.getGiftType().equals("2")&&sendStore.getEggsNum().compareTo(userTrasRecord.getGiftNum())==-1){
            return "帐号鸡蛋数量不足！";
        }
        BigDecimal giftNum = userTrasRecord.getGiftNum();

        if(userTrasRecord.getGiftType().equals("1")){
            sendStore.setChicksNum(sendStore.getChicksNum().subtract(giftNum));
        }

        if(userTrasRecord.getGiftType().equals("2")){
            sendStore.setEggsNum(sendStore.getEggsNum().subtract(giftNum));
        }
        BigDecimal feeNum= giftNum.multiply(new BigDecimal(0.1));
        giftNum = giftNum.subtract(feeNum);
        userTrasRecord.setGiftNum(giftNum);
        userTrasRecord.setFeeNum(feeNum);
        userTrasRecord.setCreateTime(StringUtils.getNowTime());
        storeMapper.updateByPrimaryKeySelective(sendStore);
        userTrasRecordMapper.insertSelective(userTrasRecord);
        return null;
    }

    @Override
    public boolean sendConfirm(UserTrasRecord userTrasRecord) {
        Store store = storeMapper.findByUserId(userTrasRecord.getReceiveId());
        if(userTrasRecord.getGiftType().equals("2")){
            BigDecimal eggsNum=store.getEggsNum().add(userTrasRecord.getGiftNum());
            store.setEggsNum(eggsNum);
        }else {
            BigDecimal chicksNum= store.getChicksNum().add(userTrasRecord.getGiftNum());
            store.setChicksNum(chicksNum);
        }
        storeMapper.updateByPrimaryKeySelective(store);
        return userTrasRecordMapper.updateByPrimaryKeySelective(userTrasRecord)>0?true:false;
    }

    public boolean receiveConfirm(UserTrasRecord userTrasRecord) {
        return userTrasRecordMapper.updateByPrimaryKeySelective(userTrasRecord)>0?true:false;
    }


    @Override
    public UserTrasRecord findById(String id) {
        return userTrasRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UserTrasRecord> findByPage(String senderId,String receiveId,int numPerPage, int currentPage) {
        int start = 0;
        start = (currentPage - 1) * numPerPage;
        return userTrasRecordMapper.findByPage(senderId,receiveId,start,numPerPage);
    }

    @Override
    public int countNum(String senderId,String receiveId) {
        return userTrasRecordMapper.countNum(senderId,receiveId);
    }

    @Override
    public List<UserTrasRecord> selectPageList(int offset, int pageSize,UserTrasRecord e) {
        return userTrasRecordMapper.selectPageList(offset,pageSize,e.getGiftType());
    }

    @Override
    public int selectPageCount(UserTrasRecord e) {
        return userTrasRecordMapper.selectPageCount(e.getGiftType());
    }
}
